package com.ruoyi.instrumentation.controller;

import java.time.LocalDate;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.instrumentation.domain.InstrumentInfo;
import com.ruoyi.instrumentation.service.IInstrumentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.instrumentation.domain.InstrumentBooking;
import com.ruoyi.instrumentation.service.IInstrumentBookingService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 仪器设备预约Controller
 * 
 * @author hj
 * @date 2025-01-13
 */
@RestController
@RequestMapping("/booking")
public class InstrumentBookingController extends BaseController
{
    @Autowired
    private IInstrumentBookingService instrumentBookingService;

    @Autowired
    private IInstrumentInfoService instrumentInfoService;

    /**
     * 查询仪器设备预约列表
     */
    @RequiresPermissions("instrumentation:booking:list")
    @GetMapping("/list")
    public TableDataInfo list(InstrumentBooking instrumentBooking)
    {
        instrumentBooking.setDelFlag(0);
        startPage();
        List<InstrumentBooking> list = instrumentBookingService.selectInstrumentBookingList(instrumentBooking);
        System.out.println(list);
        if (list != null){
            for (InstrumentBooking instrumentBooking1: list){
                InstrumentInfo instrumentInfo = instrumentInfoService.selectInstrumentInfoByInstrumentId(instrumentBooking1.getInstrumentId());
                String instrumentName = instrumentInfo.getName();
                instrumentBooking1.setInstrumentName(instrumentName);
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出仪器设备预约列表
     */
    @RequiresPermissions("instrumentation:booking:export")
    @Log(title = "仪器设备预约", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InstrumentBooking instrumentBooking)
    {
        List<InstrumentBooking> list = instrumentBookingService.selectInstrumentBookingList(instrumentBooking);
        ExcelUtil<InstrumentBooking> util = new ExcelUtil<InstrumentBooking>(InstrumentBooking.class);
        util.exportExcel(response, list, "仪器设备预约数据");
    }

    /**
     * 获取仪器设备预约详细信息
     */
    @RequiresPermissions("instrumentation:booking:query")
    @GetMapping(value = "/{bookingId}")
    public AjaxResult getInfo(@PathVariable("bookingId") Long bookingId)
    {
        return success(instrumentBookingService.selectInstrumentBookingByBookingId(bookingId));
    }

    /**
     * 新增仪器设备预约
     */
    @RequiresPermissions("instrumentation:booking:add")
    @Log(title = "仪器设备预约", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InstrumentBooking instrumentBooking)
    {
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        System.out.println("当前日期: " + currentDate);
        instrumentBooking.setBookingDate(currentDate);
        String userName = SecurityUtils.getUsername();
        instrumentBooking.setUserName(userName);
        instrumentBooking.setCreateBy(userName);
        return toAjax(instrumentBookingService.insertInstrumentBooking(instrumentBooking));
    }

    /**
     * 修改仪器设备预约
     */
    @RequiresPermissions("instrumentation:booking:edit")
    @Log(title = "仪器设备预约", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InstrumentBooking instrumentBooking)
    {
        String userName = SecurityUtils.getUsername();
        instrumentBooking.setUpdateBy(userName);
        return toAjax(instrumentBookingService.updateInstrumentBooking(instrumentBooking));
    }

    /**
     * 删除仪器设备预约
     */
    @RequiresPermissions("instrumentation:booking:remove")
    @Log(title = "仪器设备预约", businessType = BusinessType.DELETE)
	@DeleteMapping("/{bookingIds}")
    public AjaxResult remove(@PathVariable Long[] bookingIds)
    {
        //return toAjax(instrumentBookingService.deleteInstrumentBookingByBookingIds(bookingIds));

        Integer delFlag = 1;
        String username = SecurityUtils.getUsername();
        Map<String, Object> params = new HashMap<>();
        params.put("delFlag", delFlag);
        params.put("deleteBy", username);
        params.put("deleteTime", new Date());

        if (bookingIds.length == 1) {
            Long bookingId = bookingIds[0];
            params.put("bookingId", bookingId);
            return toAjax(instrumentBookingService.updateInstrumentBookingByBookingId(params));
        }
        params.put("bookingIds", bookingIds);
        return toAjax(instrumentBookingService.updateInstrumentBookingByBookingIds(params));
    }

}
